package org.mini.config
{
    import com.shine.crypt.EventDispatcherBase;
    import com.shine.manager.StageManager;
    import com.shine.utils.StringUtil;
    
    import flash.errors.IllegalOperationError;
    import flash.events.Event;
    import flash.system.Security;
    import flash.system.System;
    import flash.utils.Dictionary;
    
    import org.mini.events.ResizeEvent;
    
	[Event(name="screenResize",type="com.mini.events.ResizeEvent")]
	
	public class AppConfig
	{
		public static var swfPath:String="";
		public static var rootPath:String="";
		
		public static var staticFileVersion:String="";
		public static var picDomain:Boolean;
		public static var nodes:Dictionary;
		
		public static var screenWidth:uint;
		public static var screenHeight:uint;
		
		private static var minStageWidth:uint;
		private static var minStageHeight:uint;
		
		private static var _instance:AppConfig;
		public static function getInstance():AppConfig
		{
			if (_instance == null)	_instance = new AppConfig(new SingletonEnforcer());
			return _instance;
		}
		
		public function AppConfig(enforcer:SingletonEnforcer)
		{
			if (_instance)
			{
				throw new IllegalOperationError("Singleton: accessed only by AppConfig.getInstance()");
			}
		}
		
		public function parse(xml:XML):void
		{
			for each(var node:XML in xml.*){
				if(node.name()=="config"){
					initConfig(node);
					continue;
				}
				
				if((nodes ||= new Dictionary())[node.name()]==undefined){
					nodes[node.name()]=cacheObject(node);
				}
			}
		}
		
		private function initConfig(xml:XML):void
		{
			StageManager.getStage().showDefaultContextMenu=(xml.showContextMenu=="true");
			StageManager.getStage().scaleMode=xml.scaleMode;
			StageManager.getStage().quality=xml.quality;
			StageManager.getStage().align=xml.align;
			
			System.useCodePage=(xml.useCodePage=="true");
			if(!StringUtil.isEmpty(xml.allowDomain)) Security.allowDomain(xml.allowDomain);
			
			rootPath=StringUtil.isEmpty(xml.rootPath) ? rootPath : xml.rootPath;
			
			minStageWidth=uint(xml.minStageWidth);
			minStageHeight=uint(xml.minStageHeight);
			
			picDomain=StringUtil.isEmpty(xml.picDomain) ? true : (xml.picDomain=="true");
			
			if(!StringUtil.isEmpty(xml.crossdomain)) Security.loadPolicyFile(xml.crossdomain);
			
			if(xml.resize=="true"){
				try
				{
					stageResizeHandler();
					StageManager.getStage().addEventListener(Event.RESIZE, stageResizeHandler,false,0,true);
				}
				catch (err:Error)
				{
					trace("AppConfig:"+err.message);
				}
			}
		}
		
		private function stageResizeHandler(e:Event=null):void
		{
			screenWidth=(StageManager.getStageWidth()==0 && StageManager.getStageHeight()==0) ? screenWidth : uint(StageManager.getStageWidth());
			screenHeight=(StageManager.getStageWidth()==0 && StageManager.getStageHeight()==0) ? screenHeight : uint(StageManager.getStageHeight());
			
			if(minStageWidth>0 && minStageHeight>0){
				screenWidth=Math.max(screenWidth,minStageWidth);
				screenHeight=Math.max(screenHeight,minStageHeight);
			}
			
			EventDispatcherBase.instance.dispatchEvent(new ResizeEvent(screenWidth,screenHeight));
		}
		
		private function cacheObject(xml:XML):Object
		{
			var i:uint=0;
			var dic:Object=new Object();
			
			for each(var node:XML in xml.*){
				if(node.hasOwnProperty("@id")){
					dic[node.@id]=node.children().toXMLString();
				}else if(node.hasOwnProperty("@key")){
					dic[node.@key]=node.children().toXMLString();
				}else{
					dic[i]=node.children().toXMLString();
					i++;
				}
			}
			
			return dic;
		}
	}
}

class SingletonEnforcer {}